package component.appointment;

import java.sql.*;
import java.time.LocalDateTime;
import java.util.Scanner;
import util.ConnectionUtils;

public class Appointment {
    public static void makeAppointment(String patientId) {
        Scanner scanner = new Scanner(System.in);
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = ConnectionUtils.getConn();
            
            // 首先验证病人ID是否存在
            String checkPatientSql = "SELECT id, name FROM user WHERE id = ?";
            pstmt = conn.prepareStatement(checkPatientSql);
            pstmt.setString(1, patientId);
            rs = pstmt.executeQuery();
            
            if (!rs.next()) {
                System.out.println("错误：未找到病人信息，请确认登录状态！");
                return;
            }
            
            // 1. 显示可用医生列表
            System.out.println("请选择科室：");
            System.out.println("1. 内科");
            System.out.println("2. 外科");
            System.out.println("3. 儿科");
            int deptChoice = scanner.nextInt();
            
            String sql = "SELECT d.id, u.name, d.doctorRank FROM doctor d " +
                        "JOIN user u ON d.user_id = u.id " +
                        "WHERE d.department = ? AND d.status = 1";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, getDepartmentName(deptChoice));
            rs = pstmt.executeQuery();

            boolean hasDoctors = false;
            System.out.println("\n可预约医生列表：");
            while (rs.next()) {
                hasDoctors = true;
                System.out.println("医生ID: " + rs.getString("id") + 
                                 " 姓名: " + rs.getString("name") + 
                                 " 职称: " + rs.getString("doctorRank"));
            }

            if (!hasDoctors) {
                System.out.println("当前科室没有可预约的医生！");
                return;
            }

            // 2. 选择医生并预约时间
            System.out.println("\n请输入要预约的医生ID：");
            String doctorId = scanner.next();
            
            // 验证医生ID是否存在且可预约
            String checkDoctorSql = "SELECT id FROM doctor WHERE id = ? AND status = 1";
            pstmt = conn.prepareStatement(checkDoctorSql);
            pstmt.setString(1, doctorId);
            rs = pstmt.executeQuery();
            
            if (!rs.next()) {
                System.out.println("错误：无效的医生ID或该医生不可预约！");
                return;
            }
            
            // 生成预约ID
            String appointmentId = "APT" + System.currentTimeMillis();
            
            // 插入预约记录
            String sql1 = "INSERT INTO appointment (id, patient_id, doctor_id, appointment_time, status, create_time) " +
                  "VALUES (?, ?, ?, ?, 0, NOW())";
            pstmt = conn.prepareStatement(sql1);
            pstmt.setString(1, appointmentId);
            pstmt.setString(2, patientId);
            pstmt.setString(3, doctorId);
            pstmt.setTimestamp(4, Timestamp.valueOf(LocalDateTime.now().plusDays(1)));
            
            int result = pstmt.executeUpdate();
            if (result > 0) {
                System.out.println("预约成功！您的预约号是：" + appointmentId);
            } else {
                System.out.println("预约失败，请稍后重试");
            }
            
        } catch (SQLException e) {
            System.err.println("预约过程中发生错误: " + e.getMessage());
            e.printStackTrace(); // 添加这行以显示详细错误信息
        } finally {
            ConnectionUtils.close(conn, pstmt, rs);
        }
    }
    
    private static String getDepartmentName(int choice) {
        switch (choice) {
            case 1: return "内科";
            case 2: return "外科";
            case 3: return "儿科";
            default: return "内科";
        }
    }
}